<?php

    /**
    * Node Controller.
    *
    * Helpful methods to make Controller.
    *
    * PHP versions 5
    * CAKEPHP versions 2.x
    * 
    * Green CMS - Content Management System and Framework Powerfull by Cakephp
    * Copyright 2012, GREEN GLOBAL CO., LTD (toancauxanh.vn)
    * 
    * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
    * Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
    *
    * Licensed under The MIT License
    * Redistributions of files must retain the above copyright notice.
    *
    * @author        Technology Lab No.I <tech1@toancauxanh.vn>
    * @link          
    * @package       NodeType.Controller
    * @since         Green v 1.0
    * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
    */
    App::uses('ViewAppController', 'View.Controller');
    App::uses('ApiController', 'Api.Controller');
    App::uses('str_utility','Vendor');   
    Class ViewController extends ViewAppController {

        /**
        * Use model class
        *
        * @var array
        */
        public $uses = array('Node.Node','View.Contact','Node.NodeTerm','Node.Term', 
            'Node.Gallery','Node.NodeType','Node.MoveNode','Node.Question','User.User');
        /**
        * Use model class
        *
        * @var array
        */
        public $helpers = array('Text', 'String','Js');       
        public $compenents= array('RequestHandler','Process');
        public static $api = false;       
        public function beforeFilter() {  
            self::$api = new ApiController();            
            parent::beforeFilter();                             
            if (in_array($this->request['action'], array('forgetPass','check_email','reg_email','login','register_email','comments','contact','bookings','search','search_result'))) 
            {            
                $this->Components->disable('Security');
            }               
        } 
        //------------------Page ----------------//
        public function captcha_image()
        {
            App::uses('captcha','Vendor/captcha');
            $captcha = new captcha();
            $captcha->show_captcha();
        }
        public function captcha()
        {
            $this->Captcha->show();
        }        
        public function login(){
            $this->layout='ajax';
            if($this->request->data){
                $email=$this->data['email'];
                $password=$this->data['pass'];
                if($email&&$password)
                {   $user=$this->User->find('first',
                        array('conditions'=>array('User.email'=>$email)));
                    if($user['User']['password']==AuthComponent::password($password))
                    {   
                        if ($this->Auth->login($user, false)) {
                            $user= $this->Auth->user();
                            $this->Session->write('user', $user['User']);
                            $this->Cookie->write('hadLogin','hadLogin');
                            $check=array('check'=>$user['User']['id']);
                        } else {
                            if ($this->request->is('post')) {
                                if (!$this->Auth->isDetector()) {
                                    $check=array('check'=>0);
                                } 
                            }
                        }
                    }else{ $check=array('check'=>0);}                    
                }else{ $check=array('check'=>0);}
            }
            echo json_encode($check);exit();
        } 
        function logout() {
            //$this->log('The user has been logout of system', LOG_LOGIN);
            $this->Auth->logout();
            $this->Session->delete('user');
            $this->Cookie->delete('hadLogin');
            $this->Session->delete('cart');
            $this->Session->delete('timeOrder');
            $this->redirect('/');
        }                                                      
        public function forgetPass(){
            $this->layout='ajax';
            if($this->data){
                $user=$this->User->find('first',
                    array('conditions'=>array('User.email'=>$this->data['email'])));
                if($user){
                    if(!$user['Information']['pre']){
                        $address=$user['User']['id'].'/'.md5($user['User']['email'].$user['User']['username']);
                        $data['Contact']['email']=$user['User']['email'];
                        $data['Contact']['content']='Xin chào Anh/Chị '.$user['User']['fullname'].'.<br/><br/>
                        Anh/Chị vừa yêu cầu lấy lại mật khẩu. Vui lòng click vào đường dẫn dưới đây để tạo một mật khẩu mới.
                        <br/> www.chosach.webdev.vn/lay-lai-mat-khau/'.$address.'<br/><br/>
                        Mọi thắc mắc xin liên hệ ngay với chúng tôi để được hỗ trợ kịp thời.<br/>Xin chân thành cảm ơn!';
                        $type=3;
                        $response=array('check'=>1);
                        $this->send_email($data,$type);
                    }else{
                        $response=array('check'=>2);
                    }

                }else{
                    $response=array('check'=>0);
                }
                echo json_encode($response);
            }
            exit();
        }
        public function getPass($id=null,$address=null){
            $this->layout='ajax';
            if($address&&$id){
                $user=$this->User->find('first',
                    array('conditions'=>array('User.id'=>$id),'recursive'=>-1));
                if($user){
                    $check_address=md5($user['User']['email'].$user['User']['username']);
                    if($check_address==$address){
                        $user['User']['password']=$this->generatePassword();
                        $user['User']['re_password']=$user['User']['password'];
                        if($this->User->save($user)){
                            $this->set(compact('user'));
                        }else{ debug($this->User->validationErrors);exit();}
                        // debug('no');exit();
                    }
                }
            }
        }

        public function check_email(){
            $this->layout='ajax';
            if($this->data){
                $check=$this->Information->find('count',
                    array('conditions'=>array('Information.email'=>$this->data['email'])));
                if($check){
                    echo '<label class="error">Email này đã được đăng ký.</label>';exit;
                }    
            }
            exit;

        }
        public function convert($str) {
            $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
            $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
            $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
            $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
            $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
            $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
            $str = preg_replace("/(đ)/", 'd', $str);
            $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
            $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
            $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
            $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
            $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
            $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
            $str = preg_replace("/(Đ)/", 'D', $str);
            $str = preg_replace("/( )/", '', $str);
            $str=  strtolower($str);
            return $str;
        }        
        public function search($keyword=null){
            $keyword = @$this->params['url']['keyword'];
            $this->set(compact('keyword'));  
            $this->set('title_for_layout','Tìm kiếm - Chosach.vn');  
        }                      
        function generatePassword($length = 8) {
            $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
            $count = mb_strlen($chars);

            for ($i = 0, $result = ''; $i < $length; $i++) {
                $index = rand(0, $count - 1);
                $result .= mb_substr($chars, $index, 1);
            }        
            return $result;
        }
        public function introductions(){
            $termAlias = $content = $title = $nodeAlias = null;
            $params = $this->request->params['pass'];
            if(!empty($params)){
                $termAlias = $params[0];
                $condsDefault = $this->Process->condsDefault();
                $fieldsDefault = $this->Process->nodeFields();
                $orderDefault = $this->Process->orderFields();
                //Lấy node type 
                $opt = array(
                    'conditions' => array('NodeType.id' => 10),
                    'find' => 'first'
                );
                $nodeType = $this->get_data('Node','NodeType',$opt);
                $title = $nodeType['NodeType']['name'];
                $opt = array(
                    'conditions' => array('Term.alias' => $termAlias),
                    'recursive'  => -1,
                    'find' => 'first'
                );
                $term = $this->get_data('Node','Term',$opt);
                if(empty($term)) {$this->redirect('/');}
                $oss = array('doi-ngu');
                if(in_array($termAlias,$oss)){
                    if(isset($params[1])){
                        $nodeAlias = $params[1];
                        $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'first',
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','Node',$opt);
                        $this->set(compact('content','term','nodeType','nodeAlias'));
                        if(!empty($content)){
                            $title .= ' | ' . $content['Node']['title'];
                            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                            $this->support_seo($seo);
                        }
                    }else{
                        $opt = array(
                            'conditions' => array('NodeTerm.term_id' => $term['Term']['id']),
                            'find' => 'all',
                            'pagination' => true,
                            'limit' => 8,
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','NodeTerm',$opt);
                        $this->set(compact('content','term','nodeType'));
                        if(!empty($title)) $title .= ' | ' . $term['Term']['name'];
                        $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                        $this->support_seo($seo);
                        $this->render('introduction_team');
                    }
                }else{
                    $opt = array(
                        'conditions' => array('NodeTerm.term_id' => $term['Term']['id']),
                        'find' => 'first',
                        'fields' => $fieldsDefault
                    );
                    $content = $this->get_data('Node','NodeTerm',$opt);
                    if(!empty($title)) $title .= ' | ' . $content['Node']['title'];
                    $this->set(compact('content','nodeType','term'));
                }
            }else{$this->redirect('/');}
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
            $this->support_seo($seo);
        }
        public function services(){
            $termAlias = $termChild = $content = $title = null;  
            $params = $this->request->params['pass'];
            if(!empty($params)){
                $termAlias = $params[0];
                if(isset($params[1])) $termChild = $params[1];
                if(empty($termChild)){$this->redirect('/');}
                $condsDefault = $this->Process->condsDefault();
                $fieldsDefault = $this->Process->nodeFields();
                $orderDefault = $this->Process->orderFields();
                //Lấy node type 
                $opt = array(
                    'conditions' => array('NodeType.id' => 12),
                    'find' => 'first'
                );
                $nodeType = $this->get_data('Node','NodeType',$opt);
                $title = $nodeType['NodeType']['name'];
                $opt = array(
                    'conditions' => array('Term.alias' => $termAlias),
                    'recursive'  => -1,
                    'find' => 'first'
                );
                $termParent = $this->get_data('Node','Term',$opt);
                if(empty($termParent)) {$this->redirect('/');}
                $opt = array(
                    'conditions' => array('Term.parent_id' => $termParent['Term']['id']),
                    'recursive'  => -1,
                    'order' => array('Term.id' => 'asc'),
                    'find' => 'first'
                );
                $firstTerm = $this->get_data('Node','Term',$opt);
                $opt = array(
                    'conditions' => array('Term.alias' => $termChild),
                    'recursive'  => -1,
                    'find' => 'first'
                );
                $term = $this->get_data('Node','Term',$opt);
                if(empty($term)) {$this->redirect('/');}
                $oss = array('visa-b-1b-2','visa-p-3','tu-van-thue-cho-nha-dau-tu-eb-5-doanh-nghiep'); 
                if(in_array($termChild,$oss)){
                    if(isset($params[2])){
                        $nodeAlias = $params[2];
                        $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'first',
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','Node',$opt);
                        $this->set(compact('content','term','nodeType','firstTerm','termParent','nodeAlias'));
                        if(!empty($content)){
                            $title = $content['Node']['title'];
                            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                            $this->support_seo($seo);
                        }
                    }else{
                        $opt = array(
                            'conditions' => array('NodeTerm.term_id' => $term['Term']['id']),
                            'find' => 'all',
                            'pagination' => true,
                            'limit' => 8,
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','NodeTerm',$opt);   
                        $this->set(compact('content','term','nodeType','termAlias','firstTerm','termParent'));
                        if(!empty($title)) $title .= ' | ' . $term['Term']['name'];
                        $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                        $this->support_seo($seo);
                        $this->render('services_parts');
                    }
                }else{
                    $opt = array(
                        'conditions' => array('NodeTerm.term_id' => $term['Term']['id']),
                        'find' => 'first',
                        'fields' => $fieldsDefault
                    );
                    $content = $this->get_data('Node','NodeTerm',$opt);
                    if(!empty($title)) $title .= ' | ' . $content['Node']['title'];
                    $this->set(compact('content','nodeType','term','termAlias','termParent','firstTerm'));
                }
            }else{$this->redirect('/');}
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
            $this->support_seo($seo);
        }
        public function project(){
            $termAlias = $content = $title = null;
            $params = $this->request->params['pass'];
            if(!empty($params)){
                $nodeAlias = $params[0];
                $condsDefault = $this->Process->condsDefault();
                $condsDefault  = array_merge($condsDefault,array('Node.node_type_id' => 2,'Node.alias' => $nodeAlias));
                $fieldsDefault = $this->Process->nodeFields();
                //Lấy node type 
                $opt = array(
                    'conditions' => array('NodeType.id' => 2),
                    'find' => 'first'
                );
                $nodeType = $this->get_data('Node','NodeType',$opt);
                $title = $nodeType['NodeType']['name'];

                $opt = array(
                    'conditions' => $condsDefault,
                    'find' => 'first',
                    'fields' => $fieldsDefault
                );
                $content = $this->get_data('Node','Node',$opt);
                if(!empty($title)) $title .= ' | ' . $content['Node']['title'];
                $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                $this->support_seo($seo);
                $this->set(compact('content','nodeType'));
            }else{$this->redirect('/');}
        }
        public function news(){
            $term_alias = $content = $title = null;
            $params = $this->request->params['pass'];
            if(!empty($params)){
                $termAlias = $params[0];
                $condsDefault = $this->Process->condsDefault();
                $fieldsDefault = $this->Process->nodeFields();
                $orderDefault = $this->Process->orderFields();
                //Lấy node type 
                $opt = array(
                    'conditions' => array('NodeType.id' => 3),
                    'find' => 'first'
                );
                $nodeType = $this->get_data('Node','NodeType',$opt);
                $title = $nodeType['NodeType']['name'];

                $opt = array(
                    'conditions' => array('Term.alias' => $termAlias),
                    'recursive'  => -1,
                    'find' => 'first'
                );
                $term = $this->get_data('Node','Term',$opt);
                if(empty($term)) {$this->redirect('/');}
                $title .= ' | ' . $term['Term']['name'];
                if(trim($termAlias)=='ban-tin-hang-thang'){
                    $fieldsDefault = array_merge($fieldsDefault,array('Node.field_attach_file'));
                    $condsDefault = array_merge($condsDefault,array('NodeTerm.term_id' => $term['Term']['id']));
                    $opt = array(
                        'conditions' => $condsDefault,
                        'find' => 'all',
                        'pagination' => true,
                        'limit' => 8,
                        'fields' => $fieldsDefault
                    );
                    $content = $this->get_data('Node','NodeTerm',$opt);
                    $this->set(compact('content','nodeType','term'));
                    $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                    $this->support_seo($seo);
                    $this->render('news_months');
                }else{
                    if(isset($params[1])){
                        $nodeAlias = $params[1];
                        $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'first',
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','Node',$opt);
                        $this->set(compact('content','term','nodeType','nodeAlias'));
                        if(!empty($content)){
                            $title .= ' | ' . $content['Node']['title'];
                            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                            $this->support_seo($seo);
                        }
                        $this->render('item_detail');
                    }else{
                        $opt = array(
                            'conditions' => array('NodeTerm.term_id' => $term['Term']['id']),
                            'find' => 'all',
                            'pagination' => true,
                            'limit' => 8,
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','NodeTerm',$opt);
                        $this->set(compact('content','nodeType','term'));
                    }
                }
            }else{$this->redirect('/');}
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
            $this->support_seo($seo);
        }
        public function contact(){
            $this->layout = 'contact';
            $content = array();
            $condsDefault = $this->Process->condsDefault();
            $condsDefault  = array_merge($condsDefault,array('Node.node_type_id' => 13));
            $fieldsDefault = $this->Process->nodeFields();
            $opt = array(
                'conditions' => $condsDefault,
                'find' => 'all',
                'fields' => $fieldsDefault
            );
            $content = $this->get_data('Node','Node',$opt);
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = __d('default',"Liên hệ");
            $this->support_seo($seo);
            $this->set(compact('content'));
            if(!empty($this->request->data)){
                $data = $this->request->data;
                $this->Contact->create();
                if($this->Contact->save($data)){
                    //send mail
                    try{
                        $subject_mail = '[USIS]' . ' ' . __d('default','Contact infomation');
                        $content_mail = __d('default','Chào bạn') . ': ' . $data['Contact']['name'] . '! <br/><br/>'; 
                        $content_mail .= __d('default','Chúng tôi chân thành cảm ơn ý kiến đóng góp của bạn') . '<br/>';
                        $content_mail .= __d('default','Chúng tôi sẽ sớm xem xét và xử lý trong thời gian sớm nhất'). '<br/><br/>';
                        $content_mail .= __d('default','Trân trọng !');
                        $sendmail = $this->Process->sendMail($subject_mail,$content_mail,$data['Contact']['email']);
                        if($sendmail){
                            $this->Session->setFlash(__d('system', 'Gửi thông tin liên hệ thành công !'),'success');
                            $this->redirect($this->request->here);
                        }
                        else
                        {
                            $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                        }
                    }catch(Exception $e){
                        $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                    }
                    //End
                }else{
                    $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                }
            }
        }
        public function consultant(){
            $term_alias = $content = $title = null;
            $params = $this->request->params['pass'];
            if(!empty($params)){
                $termAlias = $params[0];
                $condsDefault = $this->Process->condsDefault();
                $fieldsDefault = $this->Process->nodeFields();
                $orderDefault = $this->Process->orderFields();
                //Lấy node type 
                $opt = array(
                    'conditions' => array('NodeType.id' => 7),
                    'find' => 'first'
                );
                $nodeType = $this->get_data('Node','NodeType',$opt);
                $title = $nodeType['NodeType']['name'];

                $opt = array(
                    'conditions' => array('Term.alias' => $termAlias),
                    'recursive'  => -1,
                    'find' => 'first'
                );
                $term = $this->get_data('Node','Term',$opt);
                if(empty($term)) {$this->redirect('/');}
                if(trim($termAlias)=='huong-dan-dinh-cu-hoa-ky'){
                    if(isset($params[1])){
                        $nodeAlias = $params[1];
                        $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'first',
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','Node',$opt);
                        $this->set(compact('content','term','nodeType','nodeAlias'));
                        if(!empty($content)){
                            $title .= ' | ' . $content['Node']['title'];
                            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                            $this->support_seo($seo);
                        }
                    }else{
                        $opt = array(
                            'conditions' => array('NodeTerm.term_id' => $term['Term']['id']),
                            'find' => 'all',
                            'pagination' => true,
                            'limit' => 8,
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','NodeTerm',$opt);
                        $this->set(compact('content','term','nodeType','nodeAlias'));
                        if(!empty($title)) $title .= ' | ' . $term['Term']['name'];
                        $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                        $this->support_seo($seo);
                        $this->render('consultants');
                    }
                }else{
                    $opt = array(
                        'conditions' => array('NodeTerm.term_id' => $term['Term']['id']),
                        'find' => 'first',
                        'fields' => $fieldsDefault
                    );
                    $content = $this->get_data('Node','NodeTerm',$opt);
                    if(!empty($title)) $title .= ' | ' . $content['Node']['title'];
                    $this->set(compact('content','nodeType','term'));
                }
            }else{$this->redirect('/');}
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
            $this->support_seo($seo);
        }
        public function faqs(){
            $contents = $content = $termChild = array();
            $termAlias = $title = null;
            $condsDefault = $this->Process->condsDefault();
            $fieldsDefault = $this->Process->nodeFields();
            $orderDefault = $this->Process->orderFields();

            //Lay node Type cau hoi thuong gap
            $opt = array(
                'conditions' => array('NodeType.id' => 8),
                'find' => 'first'
            );
            $nodeType = $this->get_data('Node','NodeType',$opt);
            $title .= $nodeType['NodeType']['name'];
            //End

            //End
            $opt = array(
                'conditions' => array('Term.type_id' => 8),
                'order' => array('Term.id' => 'asc'),
                'find' => 'all'
            );
            $term = $this->get_data('Node','Term',$opt);
            $params = $this->request->params['pass'];
            if(!empty($term)){
                if(empty($params)){
                    $opt = array(
                        'conditions' => array('Node.node_type_id' => 8),
                        'find' => 'all',
                        'pagination' => true,
                        'limit' => 10,
                        'fields' => $fieldsDefault
                    );
                    $content = $this->get_data('Node','Node',$opt);
                }else{
                    $termAlias = $params[0];
                    //End
                    $opt = array(
                        'conditions' => array('Term.alias' => $termAlias),
                        'find' => 'first'
                    );
                    $termChild = $this->get_data('Node','Term',$opt);
                    if(empty($termChild)) $this->redirect('/');
                    if(!empty($title)) $title .= ' | ' . $termChild['Term']['name'];
                    $opt = array(
                        'conditions' => array('NodeTerm.term_id' => $termChild['Term']['id']),
                        'find' => 'all',
                        'pagination' => true,
                        'limit' => 10,
                        'fields' => $fieldsDefault
                    );
                    $content = $this->get_data('Node','NodeTerm',$opt);
                }
            }
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
            $this->support_seo($seo);
            $this->set(compact('content','nodeType','term','termAlias','termChild'));
        }
        public function customer_share(){
            $content = $title = null;
            $params = $this->request->params['pass'];
            $condsDefault = $this->Process->condsDefault();
            $fieldsDefault = $this->Process->nodeFields();
            $orderDefault = $this->Process->orderFields();
            //Lấy node type tu van
            $opt = array(
                'conditions' => array('NodeType.id' => 7),
                'find' => 'first'
            );
            $nodeTypeSolution = $this->get_data('Node','NodeType',$opt);
            $title .= $nodeTypeSolution['NodeType']['name']; 

            //Lấy node type khach hang chia se
            $opt = array(
                'conditions' => array('NodeType.id' => 9),
                'find' => 'first'
            );
            $nodeType = $this->get_data('Node','NodeType',$opt);
            if(!empty($title)) $title .= ' | ' . $nodeType['NodeType']['name'];
            if(empty($nodeType)) {$this->redirect('/');}
            if(isset($params[0])){
                $nodeAlias = $params[0];
                $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                $opt = array(
                    'conditions' => $condsDefault,
                    'find' => 'first',
                    'fields' => $fieldsDefault
                );
                $content = $this->get_data('Node','Node',$opt);
                $this->set(compact('content','nodeType','nodeAlias','nodeTypeSolution'));
                if(!empty($content)){
                    $title .= ' | ' . $content['Node']['title'];
                    $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                    $this->support_seo($seo);
                }
                $this->render('customer_share_detail');
            }else{
                $opt = array(
                    'conditions' => array('Node.node_type_id' => 9),
                    'find' => 'all',
                    'pagination' => true,
                    'limit' => 8,
                    'order' => $orderDefault,
                    'fields' => $fieldsDefault
                );
                $content = $this->get_data('Node','Node',$opt);
                $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                $this->support_seo($seo);
                $this->set(compact('content','nodeType','nodeTypeSolution'));
            }
        }
        public function live_american(){
            $term_alias = $content = $title = null;
            $params = $this->request->params['pass'];
            if(!empty($params)){
                $term_alias = $params[0];
                $condsDefault = $this->Process->condsDefault();
                $fieldsDefault = $this->Process->nodeFields();
                $orderDefault = $this->Process->orderFields();
                //Lấy node type 
                $opt = array(
                    'conditions' => array('NodeType.id' => 14),
                    'find' => 'first'
                );
                $nodeType = $this->get_data('Node','NodeType',$opt);
                $title = $nodeType['NodeType']['name'];

                $opt = array(
                    'conditions' => array('Term.alias' => $term_alias),
                    'recursive'  => -1,
                    'find' => 'first'
                );
                $term = $this->get_data('Node','Term',$opt);
                if(empty($term)) {$this->redirect('/');}
                if(trim($term_alias)=='lich-su-my'){
                    if(isset($params[1])){
                        $nodeAlias = $params[1];
                        $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'first',
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','Node',$opt);
                        $this->set(compact('content','term','nodeType','nodeAlias'));
                        if(!empty($content)){
                            $title .= ' | ' . $content['Node']['title'];
                            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                            $this->support_seo($seo);
                        }
                    }else{
                        $opt = array(
                            'conditions' => array('NodeTerm.term_id' => $term['Term']['id']),
                            'find' => 'all',
                            'pagination' => true,
                            'order' => $orderDefault,
                            'limit' => 8,
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','NodeTerm',$opt);
                        $this->set(compact('content','term','nodeType','nodeAlias'));
                        if(!empty($title)) $title .= ' | ' . $term['Term']['name'];
                        $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                        $this->support_seo($seo);
                        $this->render('life_list');
                    }
                }
                elseif(trim($term_alias)=='he-thong-giao-duc-my'){
                    if(isset($params[1])){
                        $nodeAlias = $params[1];
                        $fieldsDefault = array_merge($fieldsDefault,array('Node.field_has_video'));
                        $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'first',
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','Node',$opt);
                        //Lay cac video khac
                        $condsDefault = array('Node.is_published' => 1, 'NodeTerm.term_id' => $term['Term']['id'],'Node.alias <>' => $nodeAlias);
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'all',
                            'limit' => 6,
                            'order' => $orderDefault,
                            'fields' => $fieldsDefault
                        );
                        $others_content = $this->get_data('Node','NodeTerm',$opt);
                        //End
                        if(!empty($content)){
                            $title .= ' | ' . $content['Node']['title'];
                            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                            $this->support_seo($seo);
                        }
                        $this->set(compact('content','term','nodeType','nodeAlias','others_content'));
                        $this->render('education_american_detail');
                    }else{
                        $fieldsDefault = array_merge($fieldsDefault,array('Node.field_has_video'));
                        $condsDefault = array_merge($condsDefault,array('NodeTerm.term_id' => $term['Term']['id']));
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'all',
                            'pagination' => true,
                            'order' => $orderDefault,
                            'limit' => 8,
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','NodeTerm',$opt);
                        $this->set(compact('content','term','nodeType','nodeAlias'));
                        if(!empty($title)) $title .= ' | ' . $term['Term']['name'];
                        $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                        $this->support_seo($seo);
                        $this->render('education_american');
                    }
                }
                elseif(trim($term_alias)=='danh-lam-thang-canh-my'){
                    $nodeAlias = null;
                    $fieldsDefault = array_merge($fieldsDefault,array('Node.field_has_video'));      
                    if(isset($params[1])){
                        $nodeAlias = $params[1]; 
                        $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'first',
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','Node',$opt);
                    }else{
                        $condsDefault = array_merge($condsDefault,array('NodeTerm.term_id' => $term['Term']['id']));
                        $opt = array(
                            'conditions' => $condsDefault,
                            'find' => 'first',
                            'order' => $orderDefault,
                            'fields' => $fieldsDefault
                        );
                        $content = $this->get_data('Node','NodeTerm',$opt);
                    }
                    //Lay cac video khac
                    $condsDefault = array('Node.is_published' => 1, 'NodeTerm.term_id' => $term['Term']['id'],'Node.alias <>' => $nodeAlias);
                    $opt = array(
                        'conditions' => $condsDefault,
                        'find' => 'all',
                        'limit' => 6,
                        'order' => $orderDefault,
                        'fields' => $fieldsDefault
                    );
                    $others_content = $this->get_data('Node','NodeTerm',$opt);
                    //End
                    if(!empty($content)){
                        $title .= ' | ' . $content['Node']['title'];
                        $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                        $this->support_seo($seo);
                    }
                    $this->set(compact('content','term','nodeType','nodeAlias','others_content'));
                    $this->render('sight_detail');
                }
                else{
                    $opt = array(
                        'conditions' => array('NodeTerm.term_id' => $term['Term']['id']),
                        'find' => 'first',
                        'fields' => $fieldsDefault
                    );
                    $content = $this->get_data('Node','NodeTerm',$opt);
                    if(!empty($title)) $title .= ' | ' . $content['Node']['title'];
                    $this->set(compact('content','nodeType','term'));
                }
            }else{$this->redirect('/');}
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
            $this->support_seo($seo);
        }
        public function search_result($keyword = null){
            $keyword = @$this->params['url']['keyword'];
            $this->set(compact('keyword','keyword'));
        }
        public function item_detail($nodeAlias = null){
            $title = null;
            if(!empty($nodeAlias)){
                $condsDefault = $this->Process->condsDefault();
                $fieldsDefault = $this->Process->nodeFields();
                $orderDefault = $this->Process->orderFields();
                $opt = array(
                    'conditions' => array('Node.alias' => $nodeAlias),
                    'recursive'  => -1,
                    'find' => 'first'
                );
                $content = $this->get_data('Node','Node',$opt);
                if(!empty($content)) $title = $content['Node']['title'];
                $this->set(compact('content'));
            }else{$this->redirect('/');}
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
            $this->support_seo($seo);
        }
        public function newspaper_activities(){
            $termAlias = $content = $title = $nodeAlias = null;
            $params = $this->request->params['pass'];
            if(!empty($params)){
                $termAlias = $params[0];
                $condsDefault = $this->Process->condsDefault();
                $fieldsDefault = $this->Process->nodeFields();
                $orderDefault = $this->Process->orderFields();
                //Lấy node type 
                $opt = array(
                    'conditions' => array('NodeType.id' => 11),
                    'find' => 'first'
                );
                $nodeType = $this->get_data('Node','NodeType',$opt);
                $title = $nodeType['NodeType']['name'];
                $opt = array(
                    'conditions' => array('Term.alias' => $termAlias),
                    'recursive'  => -1,
                    'find' => 'first'
                );
                $term = $this->get_data('Node','Term',$opt);
                if(empty($term)) {$this->redirect('/');}
                $title .= ' | ' . $term['Term']['name'];
                if(isset($params[1])){
                    $nodeAlias = $params[1];
                    $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                    $opt = array(
                        'conditions' => $condsDefault,
                        'find' => 'first',
                        'fields' => $fieldsDefault
                    );
                    $content = $this->get_data('Node','Node',$opt);
                    $this->set(compact('content','term','nodeType','nodeAlias'));
                    if(!empty($content)){
                        $title .= ' | ' . $content['Node']['title'];
                        $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                        $this->support_seo($seo);
                    }
                    $this->render('newspaper_activities_detail');
                }else{
                    $condsDefault = array_merge($condsDefault,array('NodeTerm.term_id' => $term['Term']['id']));
                    $opt = array(
                        'conditions' => $condsDefault,
                        'find' => 'all',
                        'pagination' => true,
                        'limit' => 8,
                        'fields' => $fieldsDefault
                    );
                    $content = $this->get_data('Node','NodeTerm',$opt);
                    $this->set(compact('content','term','nodeType'));
                    $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                    $this->support_seo($seo);
                }
            }else{$this->redirect('/');}
        }
        public function video_usis(){
            $content = $title = $nodeAlias = null;
            $condsDefault = $this->Process->condsDefault();
            $fieldsDefault = $this->Process->nodeFields();
            $orderDefault = $this->Process->orderFields();
            $params = $this->request->params['pass'];
            //Lấy node type 
            $opt = array(
                'conditions' => array('NodeType.id' => 11),
                'find' => 'first'
            );
            $nodeType = $this->get_data('Node','NodeType',$opt);
            $title = $nodeType['NodeType']['name'];
            $opt = array(
                'conditions' => array('Term.id' => 36),
                'recursive'  => -1,
                'find' => 'first'
            );
            $term = $this->get_data('Node','Term',$opt);
            if(empty($term)) {$this->redirect('/');}
            $title .= ' | ' . $term['Term']['name'];
            if(isset($params[0])){
                $nodeAlias = $params[0];
                $condsDefault = array_merge($condsDefault,array('Node.alias' => $nodeAlias));
                $opt = array(
                    'conditions' => $condsDefault,
                    'find' => 'first',
                    'fields' => $fieldsDefault
                );
                $content = $this->get_data('Node','Node',$opt);
                if(!empty($content)){
                    $title .= ' | ' . $content['Node']['title'];
                    $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                    $this->support_seo($seo);
                }
                //Lay cac video khac
                $condsDefault = array('Node.is_published' => 1, 'NodeTerm.term_id' => 36,'Node.alias <>' => $nodeAlias);
                $opt = array(
                    'conditions' => $condsDefault,
                    'find' => 'all',
                    'limit' => 6,
                    'order' => $orderDefault,
                    'fields' => $fieldsDefault
                );
                $others_content = $this->get_data('Node','NodeTerm',$opt);
                //End
                $this->set(compact('content','term','nodeType','nodeAlias','others_content'));
                $this->render('video_usis_detail');
            }else{
                $condsDefault = array_merge($condsDefault,array('NodeTerm.term_id' => $term['Term']['id']));
                $opt = array(
                    'conditions' => $condsDefault,
                    'find' => 'all',
                    'pagination' => true,
                    'limit' => 9,
                    'order' => $orderDefault,
                    'fields' => $fieldsDefault
                );
                $content = $this->get_data('Node','NodeTerm',$opt);
                $this->set(compact('content','term','nodeType'));
                $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
                $this->support_seo($seo);
            }
        }
        public function bookings(){
            //Lấy node type 
            $opt = array(
                'conditions' => array('NodeType.id' => 15),
                'find' => 'first'
            );
            $nodeType = $this->get_data('Node','NodeType',$opt);
            if(!empty($this->request->data)){
                $data = $this->request->data;
                $data['Node']['user_id'] = 1;
                $data['Node']['node_type_id'] = 15;
                $this->Node->create();
                if($this->Node->save($data)){
                    //send mail
                    try{
                        $subject_mail = '[USIS]' . ' ' . __d('default','Book your appointment');
                        $content_mail = __d('default','Chào bạn') . ': ' . $data['Node']['title'] . '! <br/><br/>'; 
                        $content_mail .= __d('default','Chúng tôi cảm ơn bạn đã đặt lịch hẹn tư vấn đến chúng tôi') . '<br/>';
                        $content_mail .= __d('default','Chúng tôi sẽ sớm xem xét và xử lý trong thời gian sớm nhất'). '<br/><br/>';
                        $content_mail .= __d('default','Trân trọng !');
                        $sendmail = $this->Process->sendMail($subject_mail,$content_mail,$data['Node']['field_email']);
                        if($sendmail){
                            $this->Session->setFlash(__d('system', 'Đặt lịch tư vấn thành công !'),'success');
                            $this->redirect($this->request->here);
                        }
                        else
                        {
                            $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                        }
                    }catch(Exception $e){
                        $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                    }
                    //End
                }else{
                    $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                }
            }
            $title = $nodeType['NodeType']['name'];
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
            $this->support_seo($seo);
            $this->set(compact('content','nodeType'));
        }
        public function ask_requestion(){
            $condsDefault = $this->Process->condsDefault();
            $fieldsDefault = $this->Process->nodeFields();
            $orderDefault = $this->Process->orderFields();
            //Lấy node type 
            $opt = array(
                'conditions' => array('NodeType.id' => 8),
                'find' => 'first'
            );
            $nodeType = $this->get_data('Node','NodeType',$opt);
            $title = __d('default','Đặt câu hỏi');
            //Lay danh sach cac chuyen muc
            $condsDefault = array('Term.type_id' => 8);
            $opt = array(
                'conditions' => $condsDefault,
                'find' => 'list',
                'fields' => array('Term.id','Term.name'),
                'order' => array('Term.created' => 'asc')
            );
            $cat = $this->get_data('Node','Term',$opt);
            //End
            if(!empty($this->request->data)){
                $data = $this->request->data;
                $data['Node']['node_type_id'] = 8;
                $data['Node']['user_id'] = 1;
                $this->Node->create();
                if($this->Node->save($data)){
                    //Luu vao term
                    $node_id = $this->Node->id;
                    $save = array(
                        'node_id' => $node_id,
                        'term_id' =>  $data['Node']['term_id']
                    );
                    $this->NodeTerm->create();
                    if($this->NodeTerm->save($save)){
                        try{
                            $subject_mail = '[USIS]' . ' ' . __d('default','Send Question');
                            $content_mail = __d('default','Chào bạn') . ': ' . $data['Node']['title'] . '! <br/><br/>'; 
                            $content_mail .= __d('default','Chúng tôi cảm ơn bạn đã đặt câu hỏi đến chúng tôi') . '<br/>';
                            $content_mail .= __d('default','Chúng tôi sẽ sớm xem xét và phản hồi đến bạn trong thời gian sớm nhất'). '<br/><br/>';
                            $content_mail .= __d('default','Trân trọng !');
                            $sendmail = $this->Process->sendMail($subject_mail,$content_mail,$data['Node']['field_email']);
                            if($sendmail){
                                $this->Session->setFlash(__d('system', 'Đặt câu hỏi thành công !'),'success');
                                $this->redirect($this->request->here);
                            }
                            else
                            {
                                $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                            }
                        }catch(Exception $e){
                            $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                        }
                    }else{
                        $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                    }
                }else{
                    $this->Session->setFlash(__d('system', 'Có lỗi xảy ra trong quá trình gửi thông tin. Vui lòng kiểm tra lại!'),'error');
                }
            }
            $seo['title_for_layout'] = $seo['keywords_for_layout'] = $title;
            $this->support_seo($seo);
            $this->set(compact('nodeType','cat'));
        }
        public function comments(){
            $this->autoRender = false;
            $result = array('msg' => __d('default','Không gửi được thông tin'),'code' => 500);
            if(!empty($this->request->data)){
                $data = $this->request->data;
                if($this->data['Comment']['capcha']!=$this->Session->read('captcha'))
                {
                    $result = array('msg' => __d('default','Mã code không đúng'),'code' => 400);
                }else{
                    $this->loadModel('View.Comment');
                    $this->Comment->create();
                    if($this->Comment->save($data)){
                        $result = array('msg' => __d('default','Cám ơn quý khách đã quan tâm đến bài viết của chúng tôi.Chúng tôi sẽ đăng tải nhận xét của quý khách trong thời gian sớm nhất'),'code' => 200);
                    }else{
                        $result = array('msg' => __d('default','Có lỗi trong quá trình lưu dữ liệu ! Vui lòng kiểm tra lại'),'code' => 400);
                    }
                }
            }
            echo json_encode($result);
            exit;
        }
        public function register_email(){
            $this->autoRender = false;
            $result = array('msg' => __d('default','Không gửi được thông tin'),'code' => 500);
            if(!empty($this->request->data)){
                $data = $this->request->data;
                $data['Node']['user_id'] = 1;
                $data['Node']['node_type_id'] = 16;
                $this->Node->create();
                if($this->Node->save($data)){
                    $result = array('msg' => __d('default','Cám ơn quý khách đã quan tâm đến bài viết của chúng tôi.Chúng tôi sẽ đăng tải nhận xét của quý khách trong thời gian sớm nhất'),'code' => 200);
                }else{
                    $msg = current($this->Node->validationErrors); 
                    $result = array('msg' => $msg,'code' => 400);
                }
            }
            echo json_encode($result);
            exit;
        }
    }
?>